<?php

die('fichier desactive par securite (commenter cette ligne de code pour le reactiver)'); // LOCK

require('include/class/Player.class.php');
require('include/class/Record.class.php');

/* Fichiers de configuration */
//require('conf/paths.conf.php');		// Configuration des paths
require('include/conf/db.conf.php');		// Configuration de l'accès à la base de données

/* Librairies */
require('include/lib/database.lib.php');	// Classe de la base de données

$db = Database::getInstance();

function unix_timestamp($date)
{
	/// http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html
	$date = str_replace(array(' ', ':'), '-', $date);
	$c    = explode('-', $date);
	$c    = array_pad($c, 6, 0);
	array_walk($c, 'intval');
 
	return mktime($c[3], $c[4], $c[5], $c[1], $c[2], $c[0]);
}


	$sql = "SELECT * FROM `record_v1` 
				INNER JOIN `track_v1` ON record_v1.Track_ID=track_v1.Track_ID
				INNER JOIN `driver_v1` ON record_v1.Driver_ID=driver_v1.Driver_ID
	";
	$sth = $db->prepare($sql);
	//$sth->bindValue(':begin', $begin, PDO::PARAM_INT);
	$sth->execute();
	$i=0;

	if($result = $sth->fetchAll())
	{
		foreach($result as $line)
		{
			// record
			$Record=round($line['record_v1'],2);
			$Minute=intval($Record/60);
			$Second= intval($Record-$Minute*60);
			$Hundredth=round(($Record-$Minute*60-$Second)*100);
			$convertedRecord = Record::MSHToTime($Minute, $Second, $Hundredth);
			
			// driver/character
			$sql = "SELECT cid FROM `character`
					WHERE `name`= '$line[Driver_Name]'
			";
			$sth = $db->prepare($sql);
			$sth->execute();
			$driver = 0;
			echo ++$i."\t| ".Record::timeToString($convertedRecord)." \t| ".($line['Date']);
			if($result = $sth->fetch())
			{
				$driver = $result['cid'];
				echo " <br />\n";
			}
			else
			{
				echo "\t NO DRIVER \n<br />";
			}
   
			$record = new Record(Array(
				'player_id' 		=> $line['Player_ID'],
				'track_name_id' 		=> (int)($line['Track_ID']/2)+1, 
				'track_type_id' 		=> ($line['Class_ID'] == 3)? 1 : 2, //3=Course => 1; 1=Lap => 2
				'track_mode_id'		=> 1, //1=CLASSIC
				'ip' 				=> "", 
				'character_id'	 	=> $driver,
				'date' 				=> $line['Date'], // aucun souci pour la conversion date => timastamp! MySQL gère tout seul '2011-04-06' => '2011-04-06 00:00:00'
				'time' 				=> $convertedRecord,
				'penta' 			=> ($line['Driver_ID'] == 10)? 1 : 0, // 10 = Penta
				'version' 			=> 0, // 0=NC
				'state' 			=> 1 // 1=OK
			));
			
			$record->save(true);
		}
	}
	echo 'Done!';


?>